package com.txh.blog.controller.link;

import com.txh.blog.model.Link;
import com.txh.blog.service.LinkService;
import com.txh.blog.util.ResultJsonUtils;
import com.txh.blog.vo.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/link")
public class LinkController {

    @Autowired
    private LinkService linkService;
    @RequestMapping("/links")
    public String links(Model model){
        Map<String,Object> page = linkService.allLinks();
        model.addAttribute("page",page);
        return "admin/link";
    }

    @RequestMapping("/del")
    @ResponseBody
    public ResultJson delLink(Long id){
        if (id == null){
            throw new NullPointerException();
        }
        linkService.delLinkById(id);
        return ResultJsonUtils.success(null);
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResultJson addLink(Link link){
        if (link == null){
            throw new NullPointerException();
        }
        linkService.addLink(link);
        return ResultJsonUtils.success(link);
    }
}
